package com.ibm.spatiotemp.tools;

public class STException  extends Exception {
	private static final long serialVersionUID = 1L;	
	
	public STException(String message) {
		super(message);
	}
	
	public STException(Throwable t) {
		this(t.toString(), t);
	}

	public STException(String message, Throwable t) {
		super(message, t);
		this.setStackTrace(t.getStackTrace());
	}
	/*
	 * return a JSON string of the Exception
	 */
	public String toJSONString() {
		String content = getMessage();
		if (content == null) {
			return "{\"key\":\"\",\"message\":\"\"}";
		}
		int index = content.indexOf(':');
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("\"key\":\"");
		if (index != -1) {
			sb.append(content.substring(0, index));
		}
		sb.append("\",\"message\":\"");
		if (index != -1) {
			sb.append(content.substring(index +1));
		} else {
			sb.append(content);
		}
		sb.append("\"");		
		sb.append("}");		
		return sb.toString();
	}
		
	public String getKey() {
		String content = getMessage();
		if (content != null) {
			int index = content.indexOf(':');
			if (index == -1) {
				return null;
			} else {
				return content.substring(0, index);
			}
		} else {
			return null;
		}
	}
	
	public String getConent() {
		String content = getMessage();
		if (content != null) {
			int index = content.indexOf(':');
			if (index == -1) {
				return content;
			} else {
				return content.substring(index +1);
			}
		} else {
			return null;
		}
	}

}
